package com.wbcs.config.template;

import java.util.ArrayList;
import java.util.List;

import com.wbcs.config.template.tags.AbsTagInTemplate;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.component.AbsComponentType;
import com.wbcs.util.Consts_Private;
import com.wbcs.util.Tools;

public class TemplateBean
{
    private String content="";
    
    private List<AbsTagInTemplate> lstTagChildren;

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content=content;
    }

    public List<AbsTagInTemplate> getLstTagChildren()
    {
        return lstTagChildren;
    }

    public void setLstTagChildren(List<AbsTagInTemplate> lstTagChildren)
    {
        this.lstTagChildren=lstTagChildren;
    }
    
    public void addChildTag(AbsTagInTemplate tagbean)
    {
        if(lstTagChildren==null)
        {
            lstTagChildren=new ArrayList<AbsTagInTemplate>();
        }
        lstTagChildren.add(tagbean);
    }
    
    /**
     * 
     * @param rrequest
     * @param ownerComponentObj
     * @return
     */
    public String getDisplayValue(ReportRequest rrequest,AbsComponentType ownerComponentObj)
    {
        StringBuffer sbuffer=new StringBuffer();
        if(lstTagChildren==null||lstTagChildren.size()==0) return content;
        int start=lstTagChildren.get(0).getStartposition();
        int end=lstTagChildren.get(lstTagChildren.size()-1).getEndposition()+1;
        sbuffer.append(content.substring(0,start));
        int endPrev=-1;
        int startCurr=-1;
        for(AbsTagInTemplate tagObjTmp:lstTagChildren)
        {
            startCurr=tagObjTmp.getStartposition();
            if(endPrev>0&&startCurr-endPrev>1)
            {
                sbuffer.append(content.substring(endPrev+1,startCurr));
            }
            sbuffer.append(tagObjTmp.getDisplayValue(rrequest,ownerComponentObj));
            endPrev=tagObjTmp.getEndposition();
        }
        if(end<content.length()-1) sbuffer.append(content.substring(end));
        return Tools.replaceAll(sbuffer.toString(),Consts_Private.SKIN_PLACEHOLDER,rrequest.getPageskin());
    }
}

